<html>
<head><meta charset="utf-8"><title>ucg#224 · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html">ucg#224</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185237448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237448">(Jan 09 2020 at 17:53)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> do you have any examples in mind ?</p>



<a name="185237508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237508">(Jan 09 2020 at 17:54)</a>:</h4>
<p>what?</p>



<a name="185237523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237523">(Jan 09 2020 at 17:54)</a>:</h4>
<blockquote>
<p>I am very against this because it would greatly complicate the unsafe code writing experience for users of the language.</p>
</blockquote>



<a name="185237540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237540">(Jan 09 2020 at 17:54)</a>:</h4>
<p>What gets complicated by the proposal in <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/224" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/224">ucg#224</a> ?</p>



<a name="185237603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237603">(Jan 09 2020 at 17:55)</a>:</h4>
<p>For slice references, we only support <code>[T]::into_raw()</code> and <code>[T]::from_raw_parts</code>, and currently do not support neither type punning through unions, nor the usage of <code>transmute</code>.</p>



<a name="185237748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237748">(Jan 09 2020 at 17:57)</a>:</h4>
<p>The only complication I can think of is that <code>(ptr, len) != [T]::from_raw(ptr, len).into_raw()</code></p>



<a name="185237825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185237825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185237825">(Jan 09 2020 at 17:58)</a>:</h4>
<p>but I don't really know how it can show up, since if you have a ZST, all pointers to it will be <code>align_of::&lt;ZST&gt;()</code> already, and all pointer arithmetic on those does nothing (e.g. <code>zst_ptr.add(N) == zst_ptr</code>)</p>



<a name="185238409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185238409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185238409">(Jan 09 2020 at 18:03)</a>:</h4>
<p>I'm not sure if it is even possible to allocate a ZST in Rust at a different address, e.g., <code>Box&lt;ZST&gt;::new()</code> will return <code>align_of</code>, and <code>GlobalAlloc</code> cannot be used to allocate ZSTs.</p>



<a name="185238513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185238513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185238513">(Jan 09 2020 at 18:04)</a>:</h4>
<p>One can definitely call a C function that returns a pointer to a ZST at a different address, or materialize one such pointer from an usize (e.g. <code>42_usize as *const ZST</code>).</p>



<a name="185238679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185238679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185238679">(Jan 09 2020 at 18:06)</a>:</h4>
<p>You can use <code>&amp;Box::new((42u8, ()).1</code> to get a different address, but I don't believe there is a guarantee for this.</p>



<a name="185238915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185238915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185238915">(Jan 09 2020 at 18:08)</a>:</h4>
<p>not for that type, but for something like <code>#[repr(C)) struct S(u8, ())</code> we have such a guarantee I think</p>



<a name="185238930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185238930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185238930">(Jan 09 2020 at 18:08)</a>:</h4>
<p>since we do specify how the offsets are computed</p>



<a name="185239568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg%23224/near/185239568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ucg.23224.html#185239568">(Jan 09 2020 at 18:15)</a>:</h4>
<p>i replied on the github issue, i didn't want to split the discussion into two places</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>